package cn.bigway.app.weavingdesigner.main;

import java.util.EnumSet;

import javax.swing.JMenu;

import cn.bigway.app.weavingdesigner.Resources;
import cn.bigway.app.weavingdesigner.type.WdMenuItemType;
import cn.bigway.app.weavingdesigner.type.WdMenuType;
import cn.bigway.app.weavingdesigner.util.WdBaseFactory;

final class WdMenuFactory extends WdBaseFactory<JMenu, WdMenuType> {

	private static final WdMenuFactory instance = new WdMenuFactory();

	private WdMenuFactory() {
		//
	}

	public static WdMenuFactory getInstance() {
		return instance;
	}

	@Override
	protected JMenu create(WdMenuType type) {
		JMenu menu = new JMenu(Resources.getString(type, "name"));
		Integer key = Resources.getInteger(type, "mnemonic_key");
		if (key != null) {
			menu.setMnemonic(key.intValue());
		}

		menu.getAccessibleContext().setAccessibleDescription(
				Resources.getString(type, "short_description"));

		EnumSet<WdMenuItemType> menuItemTypes = EnumSet
				.allOf(WdMenuItemType.class);
		for (WdMenuItemType menuItemType : menuItemTypes) {
			if (!type.equals(getMenuType(menuItemType))) {
				continue;
			}
			menu.add(WdMenuItemFactory.getInstance().get(menuItemType));
		}

		return menu;
	}

	private WdMenuType getMenuType(WdMenuItemType type) {
		switch (type) {
		case EMIT_NEW:
		case EMIT_OPEN:
		case EMIT_SAVE:
		case EMIT_SAVE_AS:
		case EMIT_SAVE_ALL:
		case EMIT_CLOSE:
		case EMIT_CLOSE_ALL:
		case EMIT_EXIT:
			return WdMenuType.EMT_FILE;

		case EMIT_REDO:
		case EMIT_UNDO:
			return WdMenuType.EMT_EDIT;

		case EMIT_SHOW_VEIW:
			return WdMenuType.EMT_WINDOW;

		case EMIT_ABOUT:
			return WdMenuType.EMT_HELP;
		}

		return null;
	}
}
